//Komissarov Denis

package API;

import java.awt.FileDialog;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import Resources.CEnvironment;

public class CFile {

	private Object parent;

	public CFile(Object parent) {

	}

	public void save(String task, String[] tests) {

		FileDialog fDialog = new FileDialog((Frame) parent,
				CEnvironment.fileSaveTitle, FileDialog.SAVE);
	
		fDialog.setFile(CEnvironment.fileType1);
		fDialog.setDirectory(".\\");

		fDialog.setVisible(true);
		if (fDialog.getFile() != null) {
			try {
				save(fDialog.getDirectory() + fDialog.getFile(), task, tests);
			} catch (Exception e) {
				// Catch exception if any
				System.err.println("CFile: Save file error: " + e.getMessage());
			}
		}
	}

	public void save(String fileName, String task, String[] tests) {

		try {
			// Create file
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
			        new FileOutputStream(fileName), "UTF8"));

			for (int i = 0; i < task.length(); i++)
				if ((int) task.charAt(i) == 10)
					task = task.substring(0, i) + "\u00AC"
							+ task.substring(i + 1);

			out.write(task);
			out.newLine();
			for (int i = 0; i < tests.length; i++) {
				out.write(tests[i]);
				out.newLine();
			}
			// Close the output stream
			out.close();
		} catch (Exception e) {
			// Catch exception if any
			System.err.println("CFile: Save file error: " + e.getMessage());
		}

	}

	// load return String = {task text, array tests}
	public String[] load() {

		FileDialog fDialog = new FileDialog((Frame) parent,
				CEnvironment.fileLoadTitle, FileDialog.LOAD);
		fDialog.setFile(CEnvironment.fileType1);
		fDialog.setDirectory(".\\");

		fDialog.setVisible(true);
		if (fDialog.getFile() != null) {
			try {
				return load(fDialog.getDirectory() + fDialog.getFile());
			} catch (Exception e) {
				// Catch exception if any
				System.err.println("CFile: Load file error: " + e.getMessage());
			}
		}

		return null;
	}

	public String[] load(String fileName) {

		try {
			// Open the file that is the first
			// command line parameter
			FileInputStream fstream = new FileInputStream(fileName);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF8"));
			// Read File Line By Line
			String test = "";
			while (br.ready()) {
				test += br.readLine() + "\u2016";
			}

			String[] result = test.split("\\\u2016");
			// Close the input stream
			in.close();

			return result;

		} catch (Exception e) {// Catch exception if any
			System.err.println("CFile: Read file error: " + e.getMessage());
		}

		return null;
	}
}
